package com.sgb.kjwl.view.ui.fragment.pick;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickDetailInfo3Binding;
import com.sgb.kjwl.model.entity.pick.TenderInfoBasicEntity3;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.PickEvent;
import com.sgb.kjwl.view.ui.adapter.pick.PickDetailDictAdapter3;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class PickDetailInfoFragment3 extends AppFragment {

    private FragmentPickDetailInfo3Binding binding;
    private PickDetailDictAdapter3 pickDetailDictAdapter3;

    public static PickDetailInfoFragment3 newInstance() {
        PickDetailInfoFragment3 fragment = new PickDetailInfoFragment3();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_detail_info3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
    }

    private void initView() {
        if (pickDetailDictAdapter3 == null) {
            pickDetailDictAdapter3 = new PickDetailDictAdapter3(getActivity(), null);
        }
        binding.pickRecycler.setSectionedRecyclerViewAdapter(pickDetailDictAdapter3);
        binding.pickRecycler.setLoadingMoreEnabled(false);
        binding.pickRecycler.setPullRefreshEnabled(false);
        GridLayoutManager rightManager = new GridLayoutManager(getActivity(), 1);
        binding.pickRecycler.setLayoutManager(rightManager);
        binding.pickRecycler.setAdapter(pickDetailDictAdapter3);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(PickEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PICK_DETAIL_INFO) {
            List<TenderInfoBasicEntity3> tenderInfoBasicEntity3List = (List<TenderInfoBasicEntity3>) event.getMessageData();
            showInfoView(tenderInfoBasicEntity3List);
        }
    }

    private void showInfoView(List<TenderInfoBasicEntity3> tenderInfoBasicEntity3List) {
        if (tenderInfoBasicEntity3List == null) {
            binding.pickRecycler.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        if (tenderInfoBasicEntity3List.size() == 0) {
            binding.pickRecycler.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        pickDetailDictAdapter3.setData(tenderInfoBasicEntity3List);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
